﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace TPG.SharePoint.Workflow.Controls.Design
{
    static class SPDesignTimeStyleManager
    {
        delegate void ApplyStyleCallback(WebControl control);
        static Dictionary<string, ApplyStyleCallback> _callbacks;

        static SPDesignTimeStyleManager()
        {
            _callbacks = CreateStyleCallbacks();
        }

        internal static void ApplySharePointStyle(Control control)
        {
            WebControl webControl = control as WebControl;
            if (webControl != null && String.IsNullOrEmpty(webControl.CssClass) == false)
            {
                string[] cssClasses = webControl.CssClass.Split(' ');
                foreach (string appliedClass in cssClasses)
                {
                    _callbacks[appliedClass](webControl);
                }
            }
        }

        internal static void ApplySharePointStyleRecursive(Control control)
        {
            ApplySharePointStyle(control);
            foreach (Control childControl in control.Controls)
            {
                ApplySharePointStyleRecursive(childControl);
            }
        }

        static Dictionary<string, ApplyStyleCallback> CreateStyleCallbacks()
        {
            Dictionary<string, ApplyStyleCallback> callbacks = new Dictionary<string, ApplyStyleCallback>();
            callbacks.Add("ms-standardheader",
                delegate(WebControl control)
                {
                    control.Style[HtmlTextWriterStyle.Color] = "#525252";
                    control.Style[HtmlTextWriterStyle.Margin] = "0em";
                    control.Style[HtmlTextWriterStyle.FontSize] = "1em";
                    control.Style[HtmlTextWriterStyle.TextAlign] = "left";
                });
            callbacks.Add("ms-descriptiontext",
                delegate(WebControl control)
                {
                    control.Style[HtmlTextWriterStyle.Color] = "#4c4c4c";
                    control.Style[HtmlTextWriterStyle.FontSize] = "8pt";
                    control.Style[HtmlTextWriterStyle.TextAlign] = "left";
                    control.Style[HtmlTextWriterStyle.FontFamily] = "tahoma";
                });
            callbacks.Add("ms-sectionheader",
                delegate(WebControl control)
                {
                    control.Style[HtmlTextWriterStyle.Color] = "#525252";
                    control.Style[HtmlTextWriterStyle.FontFamily] = "tahoma";
                    control.Style[HtmlTextWriterStyle.FontSize] = "8pt";
                    control.Style[HtmlTextWriterStyle.FontWeight] = "bold";
                });
            callbacks.Add("ms-inputformdescription",
                delegate(WebControl control)
                {
                    control.Style[HtmlTextWriterStyle.Width] = "475px";
                    control.Style[HtmlTextWriterStyle.PaddingRight] = "9px";
                });
            callbacks.Add("ms-authoringcontrols",
                delegate(WebControl control)
                {
                    control.Style[HtmlTextWriterStyle.BackgroundColor] = "#ebf3ff";
                    control.Style[HtmlTextWriterStyle.FontFamily] = "tahoma";
                    control.Style[HtmlTextWriterStyle.FontSize] = "8pt";
                    control.Style[HtmlTextWriterStyle.Color] = "#525252";
                });
            callbacks.Add("ms-inputformcontrols",
                delegate(WebControl control)
                {
                    control.Style[HtmlTextWriterStyle.Width] = "375px";
                });
            callbacks.Add("ms-propertysheet",
                delegate(WebControl control)
                {
                    control.Style[HtmlTextWriterStyle.FontFamily] = "verdana";
                    control.Style[HtmlTextWriterStyle.FontSize] = "7em";
                    control.Style[HtmlTextWriterStyle.TextAlign] = "left";
                    control.Style[HtmlTextWriterStyle.Color] = "#4c4c4c";
                });
            callbacks.Add("ms-sectionline",
                delegate(WebControl control)
                {
                    control.Style[HtmlTextWriterStyle.BackgroundColor] = "#d9d9d9";
                });
            callbacks.Add("ms-ButtonHeightWidth",
                delegate(WebControl control)
                {
                    control.Style[HtmlTextWriterStyle.Width] = "7.5em";
                    control.Style[HtmlTextWriterStyle.FontFamily] = "tahoma";
                    control.Style[HtmlTextWriterStyle.FontSize] = "8pt";
                    control.Style[HtmlTextWriterStyle.PaddingTop] = "0.1em";
                    control.Style[HtmlTextWriterStyle.PaddingBottom] = "0.4em";
                });
            return callbacks;
        }
    }
}
